//
//  MainViewController.swift
//  weibo10
//
//  Created by 周荣硕 on 16/1/7.
//  Copyright © 2016年 grgbanking. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //点击撰写按钮 使用@objc 解决使用 private 崩溃问题
    
    @objc private func clickComposedButton(){
    
        print("点我了")
    }
    
    //MARK: --视图生命周期函数
    override func viewDidLoad() {
        super.viewDidLoad()

        //添加控制器，并不会创建 tabBar中的按钮
        //懒加载是无处不在的，所有控件都是延迟创建的
        addChildViewControllers()
        //没有创建控制器
        print(tabBar.subviews)
        
        setupComposedButton()
    }
    
    
    override func viewWillAppear(animated: Bool) {
        
        //会创建tabBar中的所有控制器对应的按钮
        super.viewWillAppear(animated)
        //将撰写按钮放到最前面
       tabBar.bringSubviewToFront(composedButton)
    }
    
//MARK ---懒加载控件
 
    private lazy var composedButton: UIButton = UIButton (
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button"
    
    )
//
//    {//自定义样式的按钮
//        let button=UIButton()
//        
//        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
//        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
//        
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
//         button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//        button.sizeToFit()
//    
//        return button
//    }()
    
    
    
    
}

//MARK:--设置界面

extension MainViewController{
    
    //设置撰写按钮
    
    private func setupComposedButton(){
    
        //1.添加按钮
        tabBar.addSubview(composedButton)
        
        //调整位置
        let count=childViewControllers.count
        //让按钮宽一点点
        let w=tabBar.bounds.width / CGFloat(count)-1
        
        composedButton.frame=CGRectInset(tabBar.bounds,2*w, 0)
        //3.添加监听事件
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
        
    }
    

    //添加子控制器
    private func addChildViewControllers(){
    
              
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())

        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")

        addChildViewController(ProfilesTableViewController(), title: "我", imageName: "tabbar_profile")

        
    }

    ///添加控制器
    
    ///  添加控制器

    ///
    ///  - parameter vc:        vc
    ///  - parameter title:     标题
    ///  - parameter imageName: 图片名称
    
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
//        let vc = HomeTableViewController()
        
        vc.title=title
        
        vc.tabBarItem.image=UIImage(named: imageName)
        
        //导航控制器
        let nav=UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    
    
}
